/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.iecg.controller.app;

import com.iecg.api.dto.IecgCheckReportDto;
import com.iecg.api.dto.IecgNoticeDto;
import com.iecg.api.entity.IecgCheckReport;
import com.iecg.service.IecgCheckReportService;
import com.pig4cloud.pigx.common.core.constant.CacheConstants;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;


/**
 * 心电硬件检测管理
 *
 * @author zjc
 * @date 2019-09-05 11:07:08
 */
@RestController
@AllArgsConstructor
@RequestMapping("app/checkreport" )
@Api(value = "app_checkReport", tags = "App检测报告")
public class AppCheckReportController {

    private final IecgCheckReportService iecgCheckReportService;

    private final RedisTemplate redisTemplate;

	/**
	 * 分页查询
	 * @param
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page" )
	public R getIecgCheckReportPage(int current, int size, IecgCheckReport iecgCheckReport) {
		Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
		Pageable pageable = PageRequest.of(current-1, size, sort);
		return R.ok(iecgCheckReportService.page(pageable, iecgCheckReport));
	}



	/**
	 * 生成心电报告
	 * @param iecgCheckReportDto 生成心电报告
	 * @return R
	 */
	@ApiOperation(value = "生成心电报告", notes = "生成心电报告")
	@SysLog("生成心电报告")
	@PostMapping("/produce")
	public R produce(@RequestBody IecgCheckReportDto iecgCheckReportDto) {
		Boolean present =  iecgCheckReportService.isNotConformityRule(iecgCheckReportDto);
		if(present){
			return R.failed("采集数据量异常");
		}
		return R.ok();
	}


	/**
	 * 通过bizId查询心电报告
	 * @param bizId bizId
	 * @return R
	 */
	@ApiOperation(value = "通过bizId查询", notes = "通过bizId查询")
	@GetMapping("/{bizId}" )
	public R getByBizId(@PathVariable("bizId" ) String bizId) {
		IecgCheckReport iecgCheckReport = iecgCheckReportService.findByBizId(bizId).get();

		BoundHashOperations<String, String, IecgNoticeDto> boundHashOperations = redisTemplate.boundHashOps(CacheConstants.IECG_NOTICT + ":" +iecgCheckReport.getUserId().toString());

		if(iecgCheckReport == null){
			boundHashOperations.entries().remove(bizId);
			return R.failed("系统错误");
		}

		IecgNoticeDto iecgNoticeDto = boundHashOperations.get(bizId);
		iecgNoticeDto.setMark(false);
		boundHashOperations.put(bizId, iecgNoticeDto);
		return R.ok(iecgCheckReport);
	}

}
